home *** CD-ROM | disk | FTP | other *** search
- Path: longwood.cs.ucf.edu!not-for-mail
- From: schnitzi@longwood.cs.ucf.edu (Mark Schnitzius)
- Newsgroups: comp.lang.c
- Subject: Re: Is This Bad Coding Practice?
- Date: 30 Mar 1996 16:01:02 -0500
- Organization: University of Central Florida
- Message-ID: <4jk7ee$mtf@longwood.cs.ucf.edu>
- References: <4jgnt2$9d1@loki.tor.hookup.net>
- NNTP-Posting-Host: longwood.cs.ucf.edu
-
- Rajendra_Singh@msn.com (Rajendra Singh) writes:
-
- >int main(int argc, char *argv[])
- > {
- > char string[128];
- > char *func1(void);
-
- > strcpy(string, func1());
- > return 0;
- > }
-
- >char *func1(void)
- > {
- > char test[100];
-
- > sprintf(test, "Test: %d", 1);
- > return test;
- > }
-
- >... since I am using the value returned from func1() immediately (in
- >main()), is this reliable? After I copy it into "string", I won't be
- >using that area of memory anymore (i. e. the pointer returned by
- >func1()).
-
-
- Yes, this is bad coding practice. There's no guarantee that the
- strcpy function itself won't try to use some of the freed up memory
- allocated in func1.
-
- Why not just pass a pointer to 'string' into the function?
-
-
-
- _____________________________________________________________
- mark schnitzius - - - - - - - - - - - - - schnitzi@mentos.com
- <a href="http://east.isx.com/~schnitzi/">me</a>
- "I don't know if it's good that they did invent the wheel
- But ever since... I've been rolling on to you" --Frank Black
-